package com.orangelife.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class dom4jXml {

	/**
	 * @description 解析xml字符串
	 */

	public void readStringXml(String xml) {
		Document doc = null;
		try {

			// 读取并解析XML文档

			// SAXReader就是一个管道，用一个流的方式，把xml文件读出来

			// SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文档

			// Document document = reader.read(new File("User.hbm.xml"));

			// 下面的是通过解析xml字符串的

			doc = DocumentHelper.parseText(xml); // 将字符串转为XML

			Element rootElt = doc.getRootElement(); // 获取根节点

			System.out.println("根节点：" + rootElt.getName()); // 拿到根节点的名称

			Iterator iter = rootElt.elementIterator("request"); // 获取根节点下的子节点request

			// 遍历request节点

			while (iter.hasNext()) {

				Element recordEle = (Element) iter.next();
				String title = recordEle.elementTextTrim("title"); // 拿到head节点下的子节点title值

				System.out.println("title:" + title);

				Iterator iters = recordEle.elementIterator("script"); // 获取子节点head下的子节点script

				// 遍历Header节点下的Response节点

				while (iters.hasNext()) {

					Element itemEle = (Element) iters.next();

					String username = itemEle.elementTextTrim("username"); // 拿到head下的子节点script下的字节点username的值

					String password = itemEle.elementTextTrim("password");

					System.out.println("username:" + username);
					System.out.println("password:" + password);
				}
			}
			Iterator iterss = rootElt.elementIterator("body"); // /获取根节点下的子节点body

			// 遍历body节点

			while (iterss.hasNext()) {

				Element recordEless = (Element) iterss.next();
				String result = recordEless.elementTextTrim("result"); // 拿到body节点下的子节点result值

				System.out.println("result:" + result);

				Iterator itersElIterator = recordEless.elementIterator("form"); // 获取子节点body下的子节点form

				// 遍历Header节点下的Response节点

				while (itersElIterator.hasNext()) {

					Element itemEle = (Element) itersElIterator.next();

					String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子节点form下的字节点banlce的值

					String subID = itemEle.elementTextTrim("subID");

					System.out.println("banlce:" + banlce);
					System.out.println("subID:" + subID);
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();

		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	/**
	 * @description 将xml字符串转换成map
	 * @param xml
	 * @return Map
	 */
	public static Map<String,String> readStringXmlOut(String xml) {
		Map<String,String> map = new HashMap<String,String>();
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xml); // 将字符串转为XML

			Element rootElt = doc.getRootElement(); // 获取根节点

			System.out.println("根节点：" + rootElt.getName()); // 拿到根节点的名称

			String is_success = rootElt.elementTextTrim("is_success"); // 获取根节点下的子节点is_success
			map.put("isSuccess", is_success);
			
			String error = rootElt.elementTextTrim("error"); // 获取根节点下的子节点error
			if(!StringUtils.isBlank(error)){
				map.put("error", error);
			}
			

			Iterator response = rootElt.elementIterator("response");
			while (response.hasNext()) {
				Element recordEle = (Element) response.next();
				Iterator iter = recordEle.elementIterator("trade");
				while (iter.hasNext()) {
					Element recordEle1 = (Element) iter.next();
					String body = recordEle1.elementTextTrim("body");
					String buyer_email = recordEle1.elementTextTrim("buyer_email");
					String subject = recordEle1.elementTextTrim("subject");
					String total_fee = recordEle1.elementTextTrim("total_fee");
					String out_trade_no = recordEle1.elementTextTrim("out_trade_no");
					String trade_no = recordEle1.elementTextTrim("trade_no");
					String price = recordEle1.elementTextTrim("price");
					String quantity = recordEle1.elementTextTrim("quantity");
					String is_total_fee_adjust = recordEle1.elementTextTrim("is_total_fee_adjust");
					String trade_status = recordEle1.elementTextTrim("trade_status");
					String gmt_create = recordEle1.elementTextTrim("gmt_create");
					String gmt_payment = recordEle1.elementTextTrim("gmt_payment");
					map.put("body", body);
					map.put("buyerEmail", buyer_email);
					map.put("subject", subject);
					map.put("totalFee", total_fee);
					map.put("outTradeNo", out_trade_no);
					map.put("tradeNo", trade_no);
					map.put("price", price);
					map.put("quantity", quantity);
					map.put("isTotalFeeAdjust", is_total_fee_adjust);
					map.put("tradeStatus", trade_status);
					map.put("gmtCreate", gmt_create);
					map.put("gmtPayment", gmt_payment);
				}
			}

		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	public static void parse(String xml) {
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xml); // 将字符串转为XML

			Element rootElt = doc.getRootElement(); // 获取根节点smsReport

			Iterator iters = rootElt.elementIterator("sendResp"); // 获取根节点下的子节点sms

			while (iters.hasNext()) {
				Element recordEle1 = (Element) iters.next();
				Iterator iter = recordEle1.elementIterator("sms");
				int i = 0;
				// 遍历sms节点

				while (iter.hasNext()) {
					Element recordEle = (Element) iter.next();
					String phone = recordEle.elementTextTrim("phone"); // 拿到sms节点下的子节点stat值

					String smsID = recordEle.elementTextTrim("smsID"); // 拿到sms节点下的子节点stat值

					System.out.println(phone + "===" + smsID);
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		// 下面是需要解析的xml字符串例子

		String xmlString = "<html>" + "<head>" + "<title>dom4j解析一个例子</title>"
				+ "<script>" + "<username>yangrong</username>"
				+ "<password>123456</password>" + "</script>" + "</head>"
				+ "<body>" + "<result>0</result>" + "<form>"
				+ "<banlce>1000</banlce>" + "<subID>36242519880716</subID>"
				+ "</form>" + "</body>" + "</html>";

		/*
		 * Test2 test = new Test2(); test.readStringXml(xmlString);
		 */
		Map map = readStringXmlOut(xmlString);
		Iterator iters = map.keySet().iterator();
		while (iters.hasNext()) {
			String key = iters.next().toString(); // 拿到键

			String val = map.get(key).toString(); // 拿到值

			System.out.println(key + "=" + val);
		}
		String xml = "<batchSendResp><sendResp><sms><phone>137000000</phone><smsID>ff8080813349da9001334f0eed8c5923</smsID></sms></sendResp><sendResp><sms><phone>187000000</phone><smsID>ff8080813349da9001334f0eee045924</smsID></sms></sendResp></batchSendResp>";
		parse(xml);
	}

}